#include "reset.h"
#include "stm32f4xx.h"
#include "stm32f4xx_ll_cortex.h"
#include "stdio.h"

#define NVIC_VectTab_FLASH 0x08000000
#define NVIC_VectTab_RAM 0x20000000

void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
{
    /* Check the parameters */
    SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
}

void boot_jump(uint32_t Map, uint32_t Reset)
{
    __set_MSP(Map);
    ((void (*)())(Reset))();
}

void RebootDevice(unsigned int cause)
{
#if 0
    __set_FAULTMASK(1);//关闭所有中断
    NVIC_SystemReset();//复位函数
#endif
#if 0
    uint32_t addr = 0x08000000; // APP_ADDR0;

    uint32_t base = addr > NVIC_VectTab_FLASH ? NVIC_VectTab_FLASH : NVIC_VectTab_RAM;
    uint32_t offset = addr - base;

    // NVIC_SetVectorTable(base, offset);
    SCB->VTOR = base | (offset & (uint32_t)0x1FFFFF80);
    uint32_t map = *((uint32_t *)addr);
    uint32_t reset = *((uint32_t *)(addr + 4));
    boot_jump(map, reset);
#endif
#if 1
    __asm volatile(
        "MOV R0, #1;  "
        "MSR FAULTMASK, R0;"    // 清除FAULTMASK 禁止一切中断产生
        "LDR R0, =0xE000ED0C;"
        "LDR R1, =0x05FA0004;"
        "STR R1, [R0];"         // 系统软件复位   

        "deadloop"              // 死循环使程序运行不到下面的代码
        ":B deadloop"
    );
#endif
}

